package vn.ikame.android.plugin;

import vn.ikame.android.boojtran.truyenbua.R;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.text.Html;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;

public class INotification {
	public static final String TITLE = "5s dành cho quảng cáo";
	CustomDialog.Builder alert;
	final Handler handler = new Handler();
	private boolean isShow = false;
	private Context context;
	private String iconLink;
	private String name;
	private String content;
	private String appLink;
	private int type; // 1 là show lúc khởi động app, 2 là sau 1 khoảng thời
						// gian timeDelay, 3 là lúc thoát app
	private int timeDelay;

	public INotification(Context context, String iconLink, String name,
			String content, String appLink, int type, int timeDelay) {
		this.context = context;
		this.iconLink = iconLink;
		this.name = name;
		this.appLink = appLink;
		this.content = content;
		this.type = type;
		this.timeDelay = timeDelay;

		if (type == 1) {
			this.timeDelay = 0;
		}
	}

	public INotification(String iconLink, String name, String content,
			String appLink, int type, int timeDelay) {
		this.iconLink = iconLink;
		this.name = name;
		this.appLink = appLink;
		this.content = content;
		this.type = type;
		this.timeDelay = timeDelay;

		if (type == 1) {
			this.timeDelay = 0;
		}
	}

	public void showNotice() {
		if (type == 0)
			return;
		if (type == 2) {
			Thread thread = new Thread() {
				@Override
				public void run() {
					try {
						sleep(timeDelay * 1000);
					} catch (InterruptedException e) {
						// do nothing
					}
					handler.post(new Runnable() {

						@Override
						public void run() {
							createAlert();
						}
					});
				}
			};
			thread.start();
		} else if (type == 1) {
			createAlert();
		}
	}

	public void createAlert() {
		if (isShow) {
			return;
		}

		try {
			System.out.println("Show Notify");
			isShow = true;
			alert = new CustomDialog.Builder(context);
			alert.setMessage(content);

			SmartImageView image = (SmartImageView) alert.getView()
					.findViewById(R.id.icon);
			image.setImageUrl(iconLink);

			TextView text = (TextView) alert.getView().findViewById(R.id.name);
			text.setText(Html.fromHtml(name));

			if (!appLink.equals("")) {
				alert.setPositiveButton("Xem app",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								dialog.dismiss();
								Intent intent = new Intent(Intent.ACTION_VIEW);
								intent.setData(Uri.parse(appLink));
								context.startActivity(intent);
							}
						});
			}

			alert.setNegativeButton("Đóng",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							dialog.dismiss();
						}
					});
			alert.setCancelable(true);
			alert.show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void createNotificationAlert(final Activity atv) {
		if (isShow()) {
			return;
		}

		try {
			System.out.println("Show Notify");
			isShow = true;
			alert = new CustomDialog.Builder(atv);
			alert.setMessage(content);

			SmartImageView image = (SmartImageView) alert.getView()
					.findViewById(R.id.icon);
			image.setImageUrl(iconLink);

			TextView text = (TextView) alert.getView().findViewById(R.id.name);
			text.setText(name);

			if (!appLink.equals("")) {
				alert.setPositiveButton("Xem app",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								dialog.dismiss();
								Intent intent = new Intent(Intent.ACTION_VIEW);
								intent.setData(Uri.parse(appLink));
								atv.startActivity(intent);
							}
						});
			}

			alert.setNegativeButton("Thoát",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							dialog.dismiss();
							atv.finish();
							System.gc();
							android.os.Process.killProcess(android.os.Process
									.myPid());
						}
					});
			alert.setCancelable(true);
			alert.show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getAppLink() {
		return appLink;
	}

	public void setAppLink(String appLink) {
		this.appLink = appLink;
	}

	public boolean isShow() {
		return isShow;
	}

	public void setShow(boolean isShow) {
		this.isShow = isShow;
	}

	public String getIconLink() {
		return iconLink;
	}

	public void setIconLink(String iconLink) {
		this.iconLink = iconLink;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getTimeDelay() {
		return timeDelay;
	}

	public void setTimeDelay(int timeDelay) {
		this.timeDelay = timeDelay;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public CustomDialog.Builder getAlert() {
		return alert;
	}

	public void setAlert(CustomDialog.Builder alert) {
		this.alert = alert;
	}

}
